﻿using ConfClient;
using Zpb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UtilWpf.Extensions;
using UtilLib;
using UtilPublic.Serialization;

namespace client
{
    /// <summary>
    /// PageGm.xaml 的交互逻辑
    /// </summary>
    public partial class PageGm : Page
    {
        private static readonly string GmToken = "7u8i9o0p";
        public PageGm()
        {
            InitializeComponent();

            ComboBoxPlayerEditMoneyType.ComboBoxBindEnum<EMoneyType>();
            foreach (var item in Conf.ConfItem.Values)
            {
                var node = new ComboBoxItem();
                node.Content = item.Name;
                node.Tag = item.Id;
                ComboBoxPlayerEditItemName.Items.Add(node);
            }
            foreach (var item in Conf.ConfEquip.Values)
            {
                var node = new ComboBoxItem();
                node.Content = item.Name;
                node.Tag = item.Id;
                ComboBoxPlayerEditEquipName.Items.Add(node);
            }
            {
                List<PbAward> list = new();
                list.Add(new PbAward());
                TextBoxMialpAwards.Text = "[] 或者 " + list.Jstr();
                TextBoxMialgAwards.Text = "[] 或者 " + list.Jstr();
            }
        }
        private void Button_Click_PlayerEdit(object sender, RoutedEventArgs e)
        {
            var Content = (sender as Button).Content.ToString();
            switch (Content)
            {
                case "货币添加":
                    {
                        C2GGmAction req = new() { Token = GmToken };
                        req.Uid = TextBoxPlayerEditUid.TextboxText;
                        req.Action = Content;
                        if (ComboBoxPlayerEditMoneyType.SelectedItem == null)
                        {
                            break;
                        }
                        Util.TryParseEnum<EMoneyType>(ComboBoxPlayerEditMoneyType.SelectedValue.ToString(), out var moneyType);
                        req.Args.Add(((int)moneyType).ToString());
                        req.Args.Add(TextBoxPlayerEditMoneyCount.Text.ToLong().ToString());
                        App.Send(req);
                    }
                    break;
                case "道具添加":
                    {
                        C2GGmAction req = new() { Token = GmToken };
                        req.Uid = TextBoxPlayerEditUid.TextboxText;
                        req.Action = Content;
                        if (ComboBoxPlayerEditItemName.SelectedItem == null)
                        {
                            break;
                        }
                        req.Args.Add((ComboBoxPlayerEditItemName.SelectedItem as ComboBoxItem).Tag.ToString());
                        req.Args.Add(TextBoxPlayerEditItemCount.Text.ToLong().ToString());
                        App.Send(req);
                    }
                    break;
                case "装备添加":
                    {
                        C2GGmAction req = new() { Token = GmToken };
                        req.Uid = TextBoxPlayerEditUid.TextboxText;
                        req.Action = Content;
                        if (ComboBoxPlayerEditEquipName.SelectedItem == null)
                        {
                            break;
                        }
                        req.Args.Add((ComboBoxPlayerEditEquipName.SelectedItem as ComboBoxItem).Tag.ToString());
                        App.Send(req);
                    }
                    break;
                //case "修改头像":
                //    {
                //        MsgInt32 req = new();
                //        req.Value = TextBoxTestArg1.Text.ToInt();
                //        App.Send(EPid.CgplayerChangeHead, req);
                //    }
                //    break;
                //case "公告列表":
                //    {
                //        ListNoticeReq req = new();
                //        req.Platform = "ios";
                //        req.Version = "1";
                //        App.Send(EPid.PlistNoticeReq, req);
                //    }
                //    break;
                //case "获取玩家信息":
                //    {
                //        PlayerShowReq req = new();
                //        App.Send(EPid.PplayerShowReq, req);
                //    }
                //    break;
                //case "出战位券扣除":
                //    {
                //        BagMinusBattlePosCardReq req = new();
                //        req.NeedCardCount = 1;
                //        App.Send(EPid.PbagMinusBattlePosCardReq, req);
                //    }
                //    break;
                default:
                    break;
            }
        }
        private void Button_Click_Mial(object sender, RoutedEventArgs e)
        {
            var Content = (sender as Button).Content.ToString();
            switch (Content)
            {
                case "发送玩家邮件":
                    {
                        if (TextBoxMialpPuid.Text.IsNullOrEmpty())
                        {
                            App.Error("对象ID为空");
                            return;
                        }
                        if (TextBoxMialpTitle.Text.IsNullOrEmpty())
                        {
                            App.Error("标题为空");
                            return;
                        }
                        if (TextBoxMialpBody.Text.IsNullOrEmpty())
                        {
                            App.Error("正文为空");
                            return;
                        }
                        GmMailInfo mail = new();
                        try
                        {
                            var list = Jsh.Jobj<List<PbAward>>(TextBoxMialpAwards.Text);
                            foreach (var item in list)
                            {
                                if (Util.TryEnumIsDefined(typeof(EAwardType), item.Type))
                                {
                                    mail.Awards.Add(item);
                                }
                            }
                        }
                        catch
                        {
                            App.Error("附件格式错误");
                            return;
                        }
                        mail.Title = TextBoxMialpTitle.Text;
                        mail.Body = TextBoxMialpBody.Text;
                        C2GGmAction req = new() { Token = GmToken };
                        req.Uid = TextBoxMialpPuid.Text;
                        req.Action = Content;
                        req.Args.Add(mail.Jstr());
                        App.Send(req);
                    }
                    break;
                case "发送全服邮件":
                    {
                        if (TextBoxMialgTitle.Text.IsNullOrEmpty())
                        {
                            App.Error("标题为空");
                            return;
                        }
                        if (TextBoxMialgBody.Text.IsNullOrEmpty())
                        {
                            App.Error("正文为空");
                            return;
                        }
                        GmGMailInfo mail = new();
                        try
                        {
                            var list = Jsh.Jobj<List<PbAward>>(TextBoxMialgAwards.Text);
                            foreach (var item in list)
                            {
                                if (Util.TryEnumIsDefined(typeof(EAwardType), item.Type))
                                {
                                    mail.Awards.Add(item);
                                }
                            }
                        }
                        catch
                        {
                            App.Error("附件格式错误");
                            return;
                        }
                        mail.Title = TextBoxMialgTitle.Text;
                        mail.Body = TextBoxMialgBody.Text;
                        mail.SenderTime = MialgSendTime.ValueDateTime.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
                        C2GGmAction req = new() { Token = GmToken };
                        req.Action = Content;
                        req.Args.Add(mail.Jstr());
                        App.Send(req);
                    }
                    break;
                default:
                    break;
            }
        }
        private void Button_Click_ServerCmd(object sender, RoutedEventArgs e)
        {
            var Content = (sender as Button).Content.ToString();
            switch (Content)
            {
                case "服务器存档":
                case "服务器关闭":
                case "服务器锁定":
                case "服务器解锁":
                    {
                        C2GGmAction req = new() { Token = GmToken };
                        req.Action = Content;
                        req.Args.Add(PasswordBoxGmCmdToken.Password);
                        App.Send(req);
                    }
                    break;
                default:
                    break;
            }
        }


    }
}
